package com.mytrip.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mytrip.base.dao.impl.BaseDaoImpl;
import com.mytrip.dao.ModuleDao;
import com.mytrip.dto.ModuleDto;
import com.mytrip.model.BackOperator;
import com.mytrip.model.Module;
import com.mytrip.webapp.filter.CtxSessionBag;

public class ModuleDaoImpl extends BaseDaoImpl<Module, Long> implements ModuleDao{

	public ModuleDaoImpl(){
		super(Module.class);
	}

	@SuppressWarnings("unchecked")
	public Module getModuleByModuleName(String moduleName){
		// Map<String, Object> paras = new HashMap<String, Object>();
		// paras.put("moduleName", moduleName);
		List<Module> list = getSqlMapClientTemplate().queryForList("getModuleByModuleName", moduleName);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Module> findModuleByParentId(Long parentId){
		Map<String, Object> paras = new HashMap<String, Object>();
		paras.put("parentId", parentId);
		return getSqlMapClientTemplate().queryForList("findModuleByParentId", paras);
	}

	@SuppressWarnings("unchecked")
	public List<Module> findTopModuleByBackOperatorId(Long backOperatorId){
		Map<String, Object> paras = new HashMap<String, Object>();
		paras.put("backOperatorId", backOperatorId);
		return getSqlMapClientTemplate().queryForList("findTopModuleByBackOperatorId", paras);
	}

	@SuppressWarnings("unchecked")
	public List<Module> findModuleByParentIdnOperatorId(Long parentId, Long backOperatorId){
		ModuleDto moduleDto = new ModuleDto();
		moduleDto.setParentModuleId(parentId);
		moduleDto.setBackOperatorId(backOperatorId);
		return getSqlMapClientTemplate().queryForList("findModuleByParentIdnOperatorId", moduleDto);
	}

	public Module getModuleByActionName(String actionName){
		Map<String, Object> paras = new HashMap<String, Object>();
		paras.put("actionName", actionName);
		return (Module) getSqlMapClientTemplate().queryForObject("getModuleByActionName", paras);
	}

	@SuppressWarnings("unchecked")
	public List<Module> findTopModule(){
		return getSqlMapClientTemplate().queryForList("findTopModule");
	}

	@SuppressWarnings("unchecked")
	public List<Module> getModuleListByBackOperatorId(Long backOperatorId){
		return getSqlMapClientTemplate().queryForList("getModuleListByBackOperatorId", backOperatorId);
	}

	@SuppressWarnings("unchecked")
	public List<Module> findAllModuleByParentId(Long parentId){
		return getSqlMapClientTemplate().queryForList("findAllModuleByParentId", parentId);
	}

	@SuppressWarnings("unchecked")
	public List<Module> findModuleByBOIdnParentIdSysId(Long parentId, Long operatorId, Long systemId){
		// ���operatorIdȡ��role_id
		BackOperator bo = (BackOperator) getSqlMapClientTemplate().queryForObject("getBackOperator", operatorId);
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("parentId", parentId);
		map.put("systemId", systemId);
		map.put("operatorId", operatorId);
		// map.put("userType", bo.getUserType().longValue());
		if (CtxSessionBag.getSessionBag().getBackOperator().getAdminSiteId().intValue() != -1) {// siteAdmin �û�
			return getSqlMapClientTemplate().queryForList("findModuleBySiteUser", map);
		} else {
			return getSqlMapClientTemplate().queryForList("findModuleByBOIdnParentIdSysId", map);
		}
	}

}
